package com.iedu.demo.lgq.controller;

import com.iedu.demo.lgq.entity.TbItem;
import com.iedu.demo.lgq.service.ItemService;
import cn.dev33.satoken.util.SaResult;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("logic/item")
@CrossOrigin
public class ItemController {

    @Autowired
    private ItemService itemService;

    // 获取所有商品
    @GetMapping("/all")
    @Operation(summary = "获取所有商品")
    public SaResult getAllItems() {
        List<TbItem> items = itemService.getAllItems();
        return SaResult.code(200).setData(items);
    }

    // 上传商品
    @PostMapping("/upload")
    @Operation(summary = "上传商品")
    public SaResult uploadItem(@RequestBody TbItem item) {
        itemService.uploadItem(item);
        return SaResult.code(200).setMsg("商品上传成功");
    }

    // 编辑商品
    @PostMapping("/edit")
    @Operation(summary = "编辑商品")
    public SaResult editItem(@RequestBody TbItem item) {
        itemService.editItem(item);
        return SaResult.code(200).setMsg("商品编辑成功");
    }

    // 删除商品
    @DeleteMapping("/delete/{itemId}")  // 修改路径参数名为 itemId
    @Operation(summary = "删除商品")
    public SaResult deleteItem(@PathVariable String itemId) {
        System.out.println("收到删除请求，商品ID: " + itemId);

        // 先查询商品是否存在
        TbItem item = itemService.getItemById(itemId);
        if (item == null) {
            return SaResult.error("商品不存在，删除失败");
        }

        // 商品存在，执行删除
        itemService.deleteItem(itemId);
        return SaResult.code(200).setMsg("商品已删除");
    }

    // 设置商品库存
    @PostMapping("/setStock")
    @Operation(summary = "设置商品库存")
    public SaResult setItemStock(@RequestParam(required = true) String id,
                                 @RequestParam(required = true) String num) {
        if (id == null || id.trim().isEmpty()) {
            return SaResult.error("商品ID不能为空");
        }
        if (num == null || !num.matches("\\d+")) {
            return SaResult.error("库存数量必须为有效数字");
        }
        itemService.updateItemStock(id, num);
        return SaResult.code(200).setMsg("商品库存已更新");
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @Operation(summary = "分页查询所有商品")
    public List<TbItem> getItemList(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "40") int pageSize) {
        return itemService.getItemList(pageNum, pageSize);
    }

    @RequestMapping(value = "/listByCategory", method = RequestMethod.GET)
    @Operation(summary = "根据类目和商品标题模糊查询商品，支持分页")
    public List<TbItem> getItemsByCategory(
            @RequestParam(value = "categoryId") String categoryId, // 类目ID
            @RequestParam(value = "keyword", required = false, defaultValue = "") String keyword, // 模糊查询关键字
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, // 页码，默认从第1页开始
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) { // 每页大小，默认每页10条记录
        return itemService.getItemsByCategory(categoryId, keyword, pageNum, pageSize);
    }

    // 模糊查询商品
    @GetMapping("/search")
    @Operation(summary = "模糊查询商品")
    public SaResult searchItems(@RequestParam(required = false) String title, @RequestParam(required = false) String status) {
        List<TbItem> items = itemService.searchItems(title, status);
        return SaResult.code(200).setData(items);
    }


}
